package org.wenhuayuan.project.main.fragment.home

import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView.LayoutParams
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import org.wenhuayuan.library.util.HiDisplayUtil
import org.wenhuayuan.project.common.util.AppGlobals
import org.wenhuayuan.project.common.view.loadUrl
import org.wenhuayuan.project.main.model.HomeBanner
import org.wenhuayuan.uilibrary.banner.core.HiBanner
import org.wenhuayuan.uilibrary.banner.core.HiBannerAdapter
import org.wenhuayuan.uilibrary.banner.core.HiBannerMo
import org.wenhuayuan.uilibrary.banner.core.IHiBanner
import org.wenhuayuan.uilibrary.hiitem.HiDataItem

class BannerItem(private val list:List<HomeBanner>):HiDataItem<List<HomeBanner>, ViewHolder>(list){
    override fun onBindData(holder: ViewHolder, position: Int) {
        val banner = holder.itemView as HiBanner
        val models = mutableListOf<HiBannerMo>()
        list.forEachIndexed { _, homeBanner ->
            val bannerMo = HiBannerMo()
            bannerMo.url = homeBanner.cover
            models.add(bannerMo)
        }
        banner.setBannerData(models)
        banner.setOnBannerClickListener(object : IHiBanner.OnBannerClickListener{
            override fun onBannerClick(
                viewHolder: HiBannerAdapter.HiBannerViewHolder,
                bannerMo: HiBannerMo,
                position: Int
            ) {
                Toast.makeText(AppGlobals.get(), "banner.setOnBannerClick\n"+bannerMo.url, Toast.LENGTH_SHORT).show()
            }

            override fun onBannerLongClick(
                viewHolder: HiBannerAdapter.HiBannerViewHolder,
                bannerMo: HiBannerMo,
                position: Int
            ) {
            }
        })
        banner.setBindAdapter { viewHolder, mo, _ ->
            val imgView = viewHolder.rootView as ImageView
            imgView.loadUrl(mo.url)
        }
    }

    override fun getItemView(parent: ViewGroup): View? {
        val banner = HiBanner(parent.context)
        val resource = parent.context.resources
        val params = LayoutParams(LayoutParams.MATCH_PARENT, HiDisplayUtil.dp2px(160f, resource))
        params.bottomMargin = HiDisplayUtil.dp2px(10f, resource)
        banner.layoutParams = params
        banner.setBackgroundColor(Color.WHITE)
        return banner
    }

}